package com.impress;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Gallery;

public class FeedGallery extends Gallery {

	/**
	 * scroll horizontal
	 */
	@SuppressWarnings("unused")
	private boolean scrollingHorizontally = false;
	
	private float mInitialX;
	private float mInitialY;
	private boolean mNeedToRebase;
	private boolean mIgnore;
	private boolean mBeginScroll;
	
	public FeedGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public FeedGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public FeedGallery(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mIgnore = false;
			mNeedToRebase = true;
			mBeginScroll = false;
			mInitialX = ev.getX();
			mInitialY = ev.getY();
			return false;
		case MotionEvent.ACTION_MOVE:{
			if(!mIgnore){
				if(!mBeginScroll){
				float deltaX = Math.abs(ev.getX()-mInitialX);
				float deltaY = Math.abs(ev.getY()-mInitialY);
				if(deltaX>10)
					mBeginScroll = true;
				else {
					return false;
				}
				if(deltaX>(1.5*deltaY)){
					mIgnore = false;
				}else{
					mIgnore = true;
				}
				}
				return !mIgnore;				
			}
			return false;
		}

		default:
			return super.onInterceptTouchEvent(ev);
		}
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
//		scrollingHorizontally = true;
//		double x = Math.abs(e1.getX()-e2.getX());
//		double y = Math.abs(e1.getY()-e2.getY());
		if(mNeedToRebase){
			mNeedToRebase = false;
			distanceX = 0;
		}
		this.requestFocus();
//		scrollingHorizontally = (x>1.73*y);
		return super.onScroll(e1, e2, distanceX, distanceY);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		Log.d("FeedGallery", "onFling--velocityX: "+velocityX);
		if(Math.abs(velocityX)>10){
			if(velocityX>0){
				velocityX = 600;
			}else{
				velocityX = -600;
			}
		}else{
			velocityX = 0;
		}
		this.requestFocus();
		return super.onFling(e1, e2, velocityX, velocityY);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			scrollingHorizontally = false;
			break;

		default:
			break;
		}
		boolean intercepted = super.onTouchEvent(event);
//		Log.d("FeedGallery", "Action: "+event.getAction()+"--Intercepted: " + intercepted);
		return intercepted;
//		return false;
	}

	@Override
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
//		if(gainFocus && this.getSelectedItem()!=null){
//			this.getSelectedItem().
//		}
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
	}
	
}
